/*   1:    */ package org.jetbrains.generate.tostring.inspection;
/*   2:    */ 
/*   3:    */

import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.*;
import com.intellij.psi.util.PropertyUtil;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.generate.tostring.GenerateToStringContext;
import org.jetbrains.generate.tostring.GenerateToStringUtils;

import java.util.Collections;
import java.util.Iterator;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */ 
/*  27:    */ public class FieldNotUsedInToStringInspection
/*  28:    */   extends AbstractToStringInspection
/*  29:    */ {
/*  30:    */   @NotNull
/*  31:    */   public String getDisplayName()
/*  32:    */   {
/*  33: 41 */     String tmp2_0 = "Field not used in 'toString()' method";
/*  34: 41 */     if (tmp2_0 == null) {
/*  35: 41 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/jetbrains/generate/tostring/inspection/FieldNotUsedInToStringInspection", "getDisplayName" }));
/*  36:    */     }
/*  37: 41 */     return tmp2_0;
/*  38:    */   }
/*  39:    */   
/*  40:    */   @NotNull
/*  41:    */   public String getShortName()
/*  42:    */   {
/*  43: 47 */     String tmp2_0 = "FieldNotUsedInToString";
/*  44: 47 */     if (tmp2_0 == null) {
/*  45: 47 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/jetbrains/generate/tostring/inspection/FieldNotUsedInToStringInspection", "getShortName" }));
/*  46:    */     }
/*  47: 47 */     return tmp2_0;
/*  48:    */   }
/*  49:    */   
/*  50:    */   public boolean runForWholeFile()
/*  51:    */   {
/*  52: 52 */     return true;
/*  53:    */   }
/*  54:    */   
/*  55:    */   @NotNull
/*  56:    */   public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder paramProblemsHolder, boolean paramBoolean)
/*  57:    */   {
/*  58: 58 */     if (paramProblemsHolder == null) {
/*  59: 58 */       throw new IllegalArgumentException(String.format("Argument %s for @NotNull parameter of %s.%s must not be null", new Object[] { "0", "org/jetbrains/generate/tostring/inspection/FieldNotUsedInToStringInspection", "buildVisitor" }));
/*  60:    */     }
/*  61: 58 */     void tmp48_45 = new FieldNotUsedInToStringVisitor(paramProblemsHolder);
/*  62: 58 */     if (tmp48_45 == null) {
/*  63: 58 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/jetbrains/generate/tostring/inspection/FieldNotUsedInToStringInspection", "buildVisitor" }));
/*  64:    */     }
/*  65: 58 */     return tmp48_45;
/*  66:    */   }
/*  67:    */   
/*  68:    */   private static class FieldNotUsedInToStringVisitor
/*  69:    */     extends JavaElementVisitor
/*  70:    */   {
/*  71:    */     private final ProblemsHolder a;
/*  72:    */     
/*  73:    */     public FieldNotUsedInToStringVisitor(ProblemsHolder paramProblemsHolder)
/*  74:    */     {
/*  75: 66 */       this.a = paramProblemsHolder;
/*  76:    */     }
/*  77:    */     
/*  78:    */     public void visitField(PsiField paramPsiField)
/*  79:    */     {
/*  80: 71 */       super.visitField(paramPsiField);
/*  81:    */     }
/*  82:    */     
/*  83:    */     public void visitMethod(PsiMethod paramPsiMethod)
/*  84:    */     {
/*  85: 76 */       super.visitMethod(paramPsiMethod);
/*  86: 77 */       String str = paramPsiMethod.getName();
/*  87: 78 */       if (!"toString".equals(str)) {
/*  88: 79 */         return;
/*  89:    */       }
/*  90: 81 */       PsiParameterList localPsiParameterList = paramPsiMethod.getParameterList();
/*  91: 82 */       if (localPsiParameterList.getParametersCount() != 0) {
/*  92: 83 */         return;
/*  93:    */       }
/*  94: 85 */       PsiType localPsiType = paramPsiMethod.getReturnType();
/*  95: 86 */       PsiClassType localPsiClassType = PsiType.getJavaLangString(paramPsiMethod.getManager(), paramPsiMethod.getResolveScope());
/*  96: 87 */       if (!localPsiClassType.equals(localPsiType)) {
/*  97: 88 */         return;
/*  98:    */       }
/*  99: 90 */       PsiClass localPsiClass = paramPsiMethod.getContainingClass();
/* 100: 91 */       if (localPsiClass == null) {
/* 101: 92 */         return;
/* 102:    */       }
/* 103: 94 */       PsiField[] arrayOfPsiField = GenerateToStringUtils.filterAvailableFields(localPsiClass, GenerateToStringContext.getConfig().getFilterPattern());
/* 104:    */       PsiMethod[] arrayOfPsiMethod;
/* 105: 97 */       if (GenerateToStringContext.getConfig().isEnableMethods()) {
/* 106: 98 */         arrayOfPsiMethod = GenerateToStringUtils.filterAvailableMethods(localPsiClass, GenerateToStringContext.getConfig().getFilterPattern());
/* 107:    */       } else {
/* 108:101 */         arrayOfPsiMethod = PsiMethod.EMPTY_ARRAY;
/* 109:    */       }
/* 110:103 */       FieldNotUsedInToStringInspection.FieldUsedVisitor localFieldUsedVisitor = new FieldNotUsedInToStringInspection.FieldUsedVisitor(arrayOfPsiField, arrayOfPsiMethod);
/* 111:104 */       paramPsiMethod.accept(localFieldUsedVisitor);
/* 112:105 */       for (Iterator localIterator = localFieldUsedVisitor.getUnusedFields().iterator(); localIterator.hasNext();)
/* 113:    */       {
/* 114:105 */         localObject1 = (PsiField)localIterator.next();
/* 115:106 */         localObject2 = ((PsiField)localObject1).getName();
/* 116:107 */         this.a.registerProblem(((PsiField)localObject1).getNameIdentifier(), "Field '" + (String)localObject2 + "' is not used in 'toString()' method", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix[] { GenerateToStringQuickFix.getInstance() });
/* 117:    */       }
/* 118:    */       Object localObject1;
/* 119:    */       Object localObject2;
/* 120:110 */       for (localIterator = localFieldUsedVisitor.getUnusedMethods().iterator(); localIterator.hasNext();)
/* 121:    */       {
/* 122:110 */         localObject1 = (PsiMethod)localIterator.next();
/* 123:111 */         localObject2 = ((PsiMethod)localObject1).getNameIdentifier();
/* 124:112 */         Object localObject3 = localObject2 == null ? localObject1 : localObject2;
/* 125:113 */         this.a.registerProblem(localObject3, "Method '" + ((PsiMethod)localObject1).getName() + "' is not used in 'toString()' method", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix[] { GenerateToStringQuickFix.getInstance() });
/* 126:    */       }
/* 127:    */     }
/* 128:    */   }
/* 129:    */   
/* 130:    */   private static class FieldUsedVisitor
/* 131:    */     extends JavaRecursiveElementVisitor
/* 132:    */   {
/* 133:121 */     private final THashSet<PsiField> a = new THashSet();
/* 134:122 */     private final THashSet<PsiMethod> b = new THashSet();
/* 135:    */     
/* 136:    */     public FieldUsedVisitor(PsiField[] paramArrayOfPsiField, PsiMethod[] paramArrayOfPsiMethod)
/* 137:    */     {
/* 138:125 */       Collections.addAll(this.a, paramArrayOfPsiField);
/* 139:126 */       Collections.addAll(this.b, paramArrayOfPsiMethod);
/* 140:    */     }
/* 141:    */     
/* 142:    */     public void visitReferenceExpression(PsiReferenceExpression paramPsiReferenceExpression)
/* 143:    */     {
/* 144:131 */       if ((this.a.isEmpty()) && (this.b.isEmpty())) {
/* 145:132 */         return;
/* 146:    */       }
/* 147:134 */       super.visitReferenceExpression(paramPsiReferenceExpression);
/* 148:135 */       PsiElement localPsiElement = paramPsiReferenceExpression.resolve();
/* 149:    */       Object localObject;
/* 150:136 */       if ((localPsiElement instanceof PsiField))
/* 151:    */       {
/* 152:137 */         localObject = (PsiField)localPsiElement;
/* 153:138 */         this.a.remove(localObject);
/* 154:    */       }
/* 155:140 */       else if ((localPsiElement instanceof PsiMethod))
/* 156:    */       {
/* 157:141 */         localObject = (PsiMethod)localPsiElement;
/* 158:142 */         if (a((PsiMethod)localObject))
/* 159:    */         {
/* 160:143 */           this.a.clear();
/* 161:144 */           this.b.clear();
/* 162:    */         }
/* 163:    */         else
/* 164:    */         {
/* 165:147 */           this.b.remove(localObject);
/* 166:148 */           PsiField localPsiField = PropertyUtil.findPropertyFieldByMember((PsiMember)localObject);
/* 167:149 */           this.a.remove(localPsiField);
/* 168:    */         }
/* 169:    */       }
/* 170:    */     }
/* 171:    */     
/* 172:    */     private static boolean a(PsiMethod paramPsiMethod)
/* 173:    */     {
/* 174:155 */       String str1 = paramPsiMethod.getName();
/* 175:156 */       PsiClass localPsiClass = paramPsiMethod.getContainingClass();
/* 176:157 */       if (localPsiClass == null) {
/* 177:158 */         return false;
/* 178:    */       }
/* 179:160 */       String str2 = localPsiClass.getQualifiedName();
/* 180:161 */       if ("getDeclaredFields".equals(str1)) {
/* 181:162 */         return "java.lang.Class".equals(str2);
/* 182:    */       }
/* 183:164 */       if ("toString".equals(str1)) {
/* 184:165 */         return ("org.apache.commons.lang.builder.ReflectionToStringBuilder".equals(str2)) || ("java.util.Objects".equals(str2));
/* 185:    */       }
/* 186:168 */       return false;
/* 187:    */     }
/* 188:    */     
/* 189:    */     public THashSet<PsiField> getUnusedFields()
/* 190:    */     {
/* 191:172 */       return this.a;
/* 192:    */     }
/* 193:    */     
/* 194:    */     public THashSet<PsiMethod> getUnusedMethods()
/* 195:    */     {
/* 196:176 */       return this.b;
/* 197:    */     }
/* 198:    */   }
/* 199:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.generate.tostring.inspection.FieldNotUsedInToStringInspection

 * JD-Core Version:    0.7.0.1

 */